﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ksp.Controller
{
    public static class ModuleParser
    {
        public static String KeyWord = "MODULE";

        private static String name = "name = ";
        private static String isEnabled = "isEnabled = ";
        private static String controlSrcStatusText = "controlSrcStatusText = ";
        private static String stateString = "stateString = ";
        private static String wheelState = "WheelState = ";


        public static Model.Module ParseModule(List<string> lines)
        {
            Model.Module module = new Model.Module();

            int lineNo = 0;
            while(lineNo < lines.Count-1)
            {
                #region Parsing
                if (lines[lineNo].StartsWith(name))
                    module.Name = lines[lineNo].Replace(name, null);
                if (lines[lineNo].StartsWith(isEnabled))
                    module.IsEnabled = lines[lineNo].Replace(isEnabled, null);
                if (lines[lineNo].StartsWith(controlSrcStatusText))
                    module.ControlSrcStatusText = lines[lineNo].Replace(controlSrcStatusText, null);
                if (lines[lineNo].StartsWith(stateString))
                    module.StateString = lines[lineNo].Replace(stateString, null);
                if (lines[lineNo].StartsWith(wheelState))
                    module.WheelState = lines[lineNo].Replace(wheelState, null);

                // Parse Events
                if (lines[lineNo] == EventParser.KeyWord)
                {
                    List<String> eventLines = SubSectionParser.ParseSubSection(lines, ref lineNo);
                    List<Model.Event> events = EventParser.ParseEvent(eventLines);
                    module.Events.AddRange(events);
                }

                // Parse Actions
                if (lines[lineNo] == ActionParser.KeyWord)
                {
                    List<String> actionLines = SubSectionParser.ParseSubSection(lines, ref lineNo);
                    List<Model.Action> actions = ActionParser.ParseAction(actionLines);
                    module.Actions.AddRange(actions);
                }
                
                #endregion
                lineNo++;
            }
            return module;
        }
    }
}
